//
//  UUProductPackageEditVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 27/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUProductPackageEditVC.h"
#import "UUHybridManager+CharterSupplier.h"
#import "UUProductManager.h"
#import "UUPermissionManager.h"

@interface UUProductPackageEditVC () <UUProductManagerObserver>

@property (nonatomic, strong) UUProductPackageModel *package;
@property (nonatomic, assign) UUProductPackageType packageType;

@end

@implementation UUProductPackageEditVC

+ (instancetype)packageEditWithPackageType:(UUProductPackageType)packageType {
    UUProductPackageEditVC *editVC = [self instanceOfAppropriate];
    editVC.packageType = packageType;
    
    return editVC;
}

+ (instancetype)packageEditWithPackageId:(NSNumber *)packageId {
    DKAssert(packageId != nil);
    
    UUProductPackageEditVC *editVC = [self instanceOfAppropriate];
    editVC.package = [GetProductManager() fetchProductPackageModelWithPackageId:packageId];
    editVC.packageType = editVC.package.type;
    return editVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (!self.editable) {
        [GetProductManager() addObserver:self];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)name {
    if (self.packageType == UUProductPackageTypeDay) {
        return @"套餐";
    } else {
        return @"线路";
    }
}

- (BOOL)isAdd {
    return self.package == nil;
}

- (BOOL)canEdit {
    return [GetPermissionManager() checkPermission:UUPermissionFlagProductReadWrite];
}

- (NSURLRequest *)editRequest {
    if (self.packageType == UUProductPackageTypeDay) {
        return GetHybridManager().productPackageEditURLRequest;
    } else {
        return GetHybridManager().productPackageLineEditURLRequest;
    }
}

- (NSURLRequest *)detailRequest {
    if (self.packageType == UUProductPackageTypeDay) {
        return GetHybridManager().productPackageURLRequest;
    } else {
        return GetHybridManager().productPackageLineURLRequest;
    }
}

- (NSDictionary *)previewFields {
    if (self.package) {
        return @{
                 @"name" : self.package.name,
                 @"desc" : self.package.desc,
                 @"min_time" : @([self.package.minTime packageLineTimeToTimestamp]),
                 @"max_time" : @([self.package.maxTime packageLineTimeToTimestamp]),
                 @"is_half_day" : @(self.package.isHalfDay),
                 @"all_day_km" : @(self.package.allDayKm),
                 @"all_day_hour" : @(self.package.allDayHour),
                 @"half_day_km" : @(self.package.halfDayKm),
                 @"half_day_hour" : @(self.package.halfDayHour),
                 };
    } else {
        return nil;
    }
}

- (UUFormBaseVC *)createEditVC {
    return [[self class] packageEditWithPackageId:self.package.id];
}

- (void)addOrUpdate:(NSDictionary *)data {
    NSString *name = data[@"name"];
    NSString *desc = data[@"desc"];
    
    NSString *minTime = nil;
    NSString *maxTime = nil;
    
    BOOL isHalfDay = NO;
    NSInteger allDayKm = 0;
    NSInteger allDayHour = 0;
    NSInteger halfDayKm = 0;
    NSInteger halfDayHour = 0;
    
    if (self.packageType == UUProductPackageTypeLine) {
        minTime = [data[@"min_time"] timestampToPackageLineTime];
        maxTime = [data[@"max_time"] timestampToPackageLineTime];
    } else {
        isHalfDay = [data[@"is_half_day"] boolValue];
        allDayKm = [data[@"all_day_km"] integerValue];
        allDayHour = [data[@"all_day_hour"] integerValue];
        halfDayKm = [data[@"half_day_km"] integerValue];
        halfDayHour = [data[@"half_day_hour"] integerValue];
    }
    
    ShowIndicator();
    WEAK_SELF();
    [GetProductManager() asyncUpdatePackageWithPackageId:self.package.id
                                             packageType:self.packageType
                                                    name:name
                                                    desc:desc
                                                 minTime:minTime
                                                 maxTime:maxTime
                                               isHalfDay:isHalfDay
                                                allDayKm:allDayKm
                                              allDayHour:allDayHour
                                               halfDayKm:halfDayKm
                                             halfDayHour:halfDayHour
                                           completeBlock:^(NSError *error) {
                                               HideIndicatorInView(weakSelf.view);
                                               
                                               if (error == nil) {
                                                   [weakSelf.navigationController popViewControllerAnimated:YES];
                                               } else {
                                                   ShowTipsOnView(error.localizedDescription, weakSelf.view);
                                               }
                                           }];
}

#pragma mark - UUProductManagerObserver methods

- (void)productManagerDidUpdatePackages {
    if (self.package) {
        self.package = [GetProductManager() fetchProductPackageModelWithPackageId:self.package.id];
        [self reloadForm];
    }
}

@end
